package com.yuge.xiaohua

import android.graphics.Color
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.widget.AbsListView
import android.widget.ListView
import android.widget.Toast
import com.yuge.xiaohua.adapter.JokeAdapter
import com.yuge.xiaohua.api.ApiManager
import com.yuge.xiaohua.model.JokeInfoEntry
import com.yuge.xiaohua.model.ResultInfoEntry
import retrofit.Callback
import retrofit.Response
import retrofit.Retrofit
import java.util.*

class MainActivity : AppCompatActivity() {

    private var lvJoke : ListView? = null
    private var refreshContainer : SwipeRefreshLayout? = null
    private var jokes : ArrayList<JokeInfoEntry>? = ArrayList()
    private var jokeAdapter : JokeAdapter? = null
    private var curPageIndex : Int = 0
    private var isLoading : Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        lvJoke = this.findViewById(R.id.lvJoke) as ListView
        refreshContainer = this.findViewById(R.id.refreshContainer) as SwipeRefreshLayout
        jokeAdapter = JokeAdapter(this@MainActivity,jokes)
        lvJoke!!.adapter = jokeAdapter;
        lvJoke!!.setOnScrollListener(object:AbsListView.OnScrollListener{
            override fun onScroll(view: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) { }

            override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {
                var length : Int = jokes!!.size
                if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && length%10 == 0
                        && length-1 == view!!.lastVisiblePosition){
                    loadJokes(true)
                }
            }
        })
        refreshContainer!!.setColorSchemeColors(Color.BLUE)
        refreshContainer!!.setOnRefreshListener({ loadJokes(false) })
        loadJokes(false)
    }

    private fun loadJokes(isLoadMore:Boolean){
        if(!isLoading) {
            isLoading = true
            var pageIndex: Int = if (isLoadMore) curPageIndex + 1 else 0
            ApiManager.getHttpApi().getJokes(10, pageIndex).enqueue(object : Callback<ResultInfoEntry<Array<JokeInfoEntry>>> {
                override fun onResponse(response: Response<ResultInfoEntry<Array<JokeInfoEntry>>>, retrofit: Retrofit) {
                    if(refreshContainer!!.isRefreshing) refreshContainer!!.isRefreshing = false
                    if (response.isSuccess && response.body() != null) {
                        var result: ResultInfoEntry<Array<JokeInfoEntry>> = response.body()
                        if (result.status.equals("000000")) {
                            if (this@MainActivity.curPageIndex == 0)
                                jokes!!.clear()
                            this@MainActivity.curPageIndex = pageIndex
                            jokes!!.addAll(result.detail!!.toArrayList())
                            jokeAdapter!!.notifyDataSetChanged()
                            Toast.makeText(this@MainActivity, if (this@MainActivity.curPageIndex == 0) "刷新完成" else "加载完成", Toast.LENGTH_LONG).show()
                        } else {
                            Toast.makeText(this@MainActivity, result!!.desc, Toast.LENGTH_SHORT).show()
                        }
                    }
                    isLoading = false
                }

                override fun onFailure(t: Throwable) {
                    if(refreshContainer!!.isRefreshing) refreshContainer!!.isRefreshing = false
                    Toast.makeText(this@MainActivity, "网络错误，请稍候重试", Toast.LENGTH_SHORT).show()
                    isLoading = false
                }
            })
        }
    }

}
